﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class manual_add_cargo : Form
    {
        public string cargo_id = "";
        public string row_index = "";   // 所在的行
        public string col_index = "";   // 所在的列
        public string shelf = "";       // 所在的货架

        public void Fill_Info()
        {
            label_row_index.Text = row_index;
            label_col_index.Text = col_index;
            label_shelf.Text = shelf;
            label_cargo_id.Text = DateTime.Now.ToString() + shelf.ToString();
        }
        public manual_add_cargo()
        {
            InitializeComponent();
        }

        private void manual_add_cargo_Load(object sender, EventArgs e)
        {
            // 添加货物状态下拉菜单
            comboBox_state.Items.Add("未成品");
            comboBox_state.Items.Add("成品");

            // 添加货物种类下拉菜单
            DataSet ds = MainView.mysql.sql_search_database("select * from cargoinfotable");
            int count = ds.Tables[0].Rows.Count;
            for(int i=0;i<count;i++)
            {
                comboBox_cargo_kind.Items.Add(ds.Tables[0].Rows[i][0].ToString());
            }

            // 添加货架下拉菜单
            //ds = MainView.mysql.sql_search_database("Select * from entrepotinfo");
            //count = ds.Tables[0].Rows.Count;
            //for(int i=0;i<count;i++)
            //{
            //    comboBox_shelf.Items.Add(ds.Tables[0].Rows[i][0].ToString());
            //}

            // 添加客户下拉菜单
            ds = MainView.mysql.sql_search_database("Select * from customertable");
            count = ds.Tables[0].Rows.Count;
            for(int i=0;i<count;i++)
            {
                comboBox_customer.Items.Add(ds.Tables[0].Rows[i][0].ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string id=label_cargo_id.Text.Trim();
                string name = textBox_name.Text.Trim();                   // 名称
                string state = comboBox_state.Text.Trim();                // 状态
                string kind = comboBox_cargo_kind.Text.Trim();            // 种类
                string shelf = label_shelf.Text.Trim();          // 货架
                int row_index = int.Parse(label_row_index.Text.Trim());         // 货物的行序号
                int col_index = int.Parse(label_col_index.Text.Trim());         // 货物的列序号
                string customer = comboBox_customer.Text.Trim();                   // 客户
                if (name == "") { MessageBox.Show("货物名称不能为空！"); return; }
                if (state == "") { MessageBox.Show("必须选择一个状态！"); return; }
                if (kind == "") { MessageBox.Show("必须选择一个货物种类！"); return; }
                if (shelf == "") { MessageBox.Show("必须选择一个货架！"); return; }
                if (customer == "") { MessageBox.Show("必须选择一个客户！"); return; }
                MainView.mysql.excute_sql("insert into cargotable values('"+id+"','"+name+"','"+kind+"','"+state+"',' ','"+shelf+"','"+row_index+"','"+col_index+"','"+customer+"','"+MainView.username+"','"+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"','manual')");
                this.DialogResult = DialogResult.OK;
                this.Dispose();
            }
            catch { MessageBox.Show("在添加过程中有错误！"); }
        }

        private void comboBox_customer_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void comboBox_customer_Click(object sender, EventArgs e)
        {
           
        }

        private void comboBox_customer_SelectedValueChanged(object sender, EventArgs e)
        {
           
        }

        private void comboBox_customer_ValueMemberChanged(object sender, EventArgs e)
        {
            try
            {
                string customer = comboBox_customer.SelectedValue.ToString().Trim();       //客户的ID
                DataSet ds = MainView.mysql.sql_search_database("Select name from customertable where customerID='" + customer + "'");
                string customername = ds.Tables[0].Rows[0][0].ToString();
                toolTip1.InitialDelay = 100;
                toolTip1.Show(customername, comboBox_customer);

            }
            catch { }
        }
    }
}
